angular.module('imgUrl', [])
    .constant('IMGURL',admin_img_url)
    .constant('IMGDEFAULTURL',admin_img_default_url)
    .value('IMGPATH',{
        p:'product/',/*商品*/
        p_norms:'product/',/*属性规格图片*/
        a:'product_attr/', /*商品属性*/
        cat:'catalog/',  //分类
        cat_icon:'catalog/',//icon
        slider:'slider/',//slider
        dashboard:'dashboard/',//dashboard
        brand:'brand/',//brand
        tags:'tags/',//tags
        advert:'advert/',//广告图
        reviews:'reviews/',//评价图

    })
    .filter('imgwhq',function(IMGURL,IMGPATH,IMGDEFAULTURL){
        var url = {};
        angular.forEach(IMGPATH,function (v,i) {
            url[i] = IMGURL+ v;
        });
        return function(date,w,h,q,max) {
            var z = '';
            if(!date||date == 'default'){
                if(!!w&&!!h&&!!q){
                    return z = IMGDEFAULTURL+'@'+Number(w)+"w_"+Number(h)+'h_'+Number(q)+'q_1e_1c';
                }else{
                    return z = IMGDEFAULTURL+'@300w_1e_1c';
                }
            }else {
                if(!!w&&!!h&&!!q){
                    //z = IMGURL+date+'@'+Number(w)+"w_"+Number(h)+'h_'+Number(q)+'q_1e_1c';//+date.match(/\.\w*?$/g)
                    z = url[date.split('_')[0]]+date+'@'+Number(w)+"w_"+Number(h)+'h_'+Number(q)+'q_1e_1c';//+date.match(/\.\w*?$/g)
                    return z;
                }else {
                    if(max){
                        z = url[date.split('_')[0]]+date+'@600w_1e_1c';
                    }else {
                        z = url[date.split('_')[0]]+date;/*+date+'@600w_1e_1c'*/
                    }
                    return z;
                }
                return date;
            }
        }
    })
    .directive("errSrc",function(IMGDEFAULTURL){   /*err-src="300:300:90"*/
        return {
            link: function(scope, element, attrs) {
                element.bind('error', function() {
                    var imgl = attrs.errSrc.split(':');
                    var z = IMGDEFAULTURL+'@'+Number(imgl[0])+"w_"+Number(imgl[1])+'h_'+Number(imgl[2])+'q_1e_1c';
                    attrs.$set('src',z);
                    element.unbind('error')
                });
            }
        }
    });